package com.jxdinfo.hussar.activitidemo.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jxdinfo.hussar.activitidemo.bean.ChatMessage;
import com.jxdinfo.hussar.activitidemo.bean.TdbYjMEvent;
import com.jxdinfo.hussar.activitidemo.bean.TdbYjMEventDetail;
import com.jxdinfo.hussar.activitidemo.bean.TdbYjMEventInteraction;
import com.jxdinfo.hussar.activitidemo.dao.TdbYjMEventDetailMapper;
import com.jxdinfo.hussar.activitidemo.dao.TdbYjMEventMapper;

/**
 * <p>
 * 应急事件服务实现类
 * </p>
 *
 * @author zzd
 * @since 2019-04-22
 */
@Service
public class TdbYjMEventServiceImpl extends ServiceImpl<TdbYjMEventMapper, TdbYjMEvent> implements ITdbYjMEventService {
	@Autowired
	TdbYjMEventMapper mapper;
	@Autowired
	TdbYjMEventDetailMapper eventDetailMapper;

	@Override
	public Map<String, Object> selectFlowByTrainId(Map<String, Object> map) {
		Map<String, Object> reMap = new HashMap<String, Object>();
		// 根据车型和代码查询流程条数
		String total = "0";
		try {
			total = mapper.selectNumFlowByTrainId(map);
		} catch (Exception e) {
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("msg", "查询数据失败，请联系管理员");
			return reMap;
		}
		if (Integer.parseInt(total) > 0) {
			reMap.put("total", total);
		} else {
			reMap.put("status", "eroor");
			reMap.put("msg", "查询数据为空");
			return reMap;
		}
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		try {
			list = mapper.selectFlowByTrainId(map);
		} catch (Exception e) {
			e.printStackTrace();
			reMap.put("status", "error");
			reMap.put("msg", "查询数据失败，请联系管理员");
			return reMap;
		}
		if (!list.isEmpty()) {
			reMap.put("data", list);
		} else {
			reMap.put("status", "eroor");
			reMap.put("msg", "查询数据为空");
			return reMap;
		}
		reMap.put("status", "success");
		return reMap;
	}

	@Override
	public String selectNumFlowByTrainId(Map<String, Object> map) {
		return mapper.selectNumFlowByTrainId(map);
	}

	@Override
	public int insertEventInfo(TdbYjMEvent event) {

		return mapper.insertEventInfo(event);
	}

	@Override
	public int updateEventInfo(TdbYjMEvent event) {

		return mapper.updateEventInfo(event);
	}

	@Override
	public int deleteEventInfo(Map<String, Object> map) {
		int num = mapper.deleteEventInfo(map);
		int num2 = mapper.deleteEventCodeInfo(map);
		return num + num2;
	}

	@Override
	public int updateEventDetail(TdbYjMEventDetail eventDetail) {
		// TODO Auto-generated method stub
		return eventDetailMapper.updateEventDetail(eventDetail);
	}

	@Override
	public int addEventDetail(TdbYjMEventDetail eventDetail) {
		// TODO Auto-generated method stub
		return eventDetailMapper.addEventDetail(eventDetail);
	}

	@Override
	public int updateEventStatus(TdbYjMEventDetail eventDetail) {
		// TODO Auto-generated method stub
		return mapper.updateEventStatus(eventDetail);
	}

	@Override
	public List<Map<String, Object>> getParentFlowNodeStatus(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return eventDetailMapper.getParentFlowNodeStatus(map);
	}

	@Override
	public void insertEventInteraction(ChatMessage message) {
		TdbYjMEventInteraction interaction = new TdbYjMEventInteraction();
		interaction.setdOperateDate(message.getSendTime());
		interaction.setsContent(message.getContent());
		interaction.setsEventDetailId(message.getsEventDetailId());
		interaction.setsEventId(message.getsEventId());
		interaction.setsFile(message.getImgUrl());
		if (message.getType() == 3) {
			// 消息类型为3时，为图片类型消息
			interaction.setsType(1);
		} else {
			// 其他消息类型均视为文本类消息
			interaction.setsType(0);
		}
		interaction.setsUser(message.getFromUserId());
		mapper.insertEventInteraction(interaction);
	}

	@Override
	public String selectNumEventInfo(Map<String, Object> map) {
		// TODO 自动生成的方法存根
		return mapper.selectNumEventInfo(map);
	}

	@Override
	public int insertEventCode(Map<String, Object> map) {
		// TODO 自动生成的方法存根
		return mapper.insertEventCode(map);
	}

	@Override
	public List<Map<String, Object>> selectEventInfo(Map<String, Object> map) {
		Page page = new Page();
		return mapper.selectEventInfo(page, map);
	}

	@Override
	public Page<ChatMessage> selectChatRecord(Page<ChatMessage> page, TdbYjMEventInteraction interaction) {
		List<ChatMessage> list = this.mapper.selectChatRecord(page, interaction);
		page.setRecords(list);
		return page;
	}

	@Override
	public Integer selectChatRecordCount(TdbYjMEventInteraction interaction) {
		return mapper.selectChatRecordCount(interaction);
	}

	@Override
	public Page<Map<String, Object>> selectEventInfo(Page<Map<String, Object>> page, Map<String, Object> map) {
		List<Map<String, Object>> list = mapper.selectEventInfo(page, map);
		page.setRecords(list);
		String total = mapper.selectNumEventInfo(map);
		page.setTotal(Integer.parseInt(total));
		return page;
	}

	@Override
	public TdbYjMEvent selectEventById(TdbYjMEvent event) {
		return mapper.selectEventById(event);
	}

	@Override
	public Integer getTrackEventCount() {
		// TODO Auto-generated method stub
		return mapper.getTrackEventCount();
	}

	@Override
	public int updateReportStatus(String sReportId, String sEventId) {
		return baseMapper.updateReportStatus(sReportId, sEventId);
	}

	@Override
	public String selectTarinSetNameById(String sTarinsetid) {
		return baseMapper.selectTarinSetNameById(sTarinsetid);
	}
}
